package com.video.service.webcon.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.video.entity.Solution;
import com.video.entity.Task;
import com.video.entity.Webcon;
import com.video.mapper.SolutionMapper;
import com.video.mapper.TaskMapper;
import com.video.service.webcon.ISolutionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class SolutionServiceImpl extends ServiceImpl<SolutionMapper, Solution> implements ISolutionService {

    @Autowired
    private SolutionMapper solutionMapper;

    @Autowired
    private TaskMapper taskMapper;

    /**
     * 根据分控创建默认方案
     * @param webcon
     * @return
     */
    @Override
    public int createDefaultSolution(Webcon webcon) {
        QueryWrapper<Solution> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("account", webcon.getAccount());
        //查询是否存在默认方案
        Integer result = solutionMapper.selectCount(queryWrapper);

        //如果不存在
        if(result == 0){
            //创建默认方案
            Solution solution = new Solution().setSolutionname("Default Solution").setAccount(webcon.getAccount()).setIsaction(1);
            return solutionMapper.insert(solution);
        }
        return 0;
    }

    /**
     * 根据分控账号 查询对应方案列表
     * @param account
     * @return
     */
    @Override
    public List<Solution> queryByAccount(String account) {
        QueryWrapper<Solution> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("account", account);
        queryWrapper.or();
        queryWrapper.isNull("account");
        List<Solution> solutions = solutionMapper.selectList(queryWrapper);
        return solutions;
    }

    /**
     * 添加方案
     * @param solution
     * @return
     */
    @Override
    @Transactional
    public int insertSolution(Solution solution) {
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("solutionname", solution.getSolutionname());
        queryWrapper.eq("account", solution.getAccount());
        Solution s = solutionMapper.selectOne(queryWrapper);
        if(s != null){
            return -1;
        }
        return solutionMapper.insert(solution);
    }

    /**
     * 修改方案名称
     * @param solution
     * @param oldName
     * @return
     */
    @Override
    @Transactional
    public int updateSolution(Solution solution, String oldName) {
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("solutionname", solution.getSolutionname());
        queryWrapper.eq("account", solution.getAccount());
        int count = solutionMapper.selectCount(queryWrapper);
        if(count > 0){
            return -1;
        }

        QueryWrapper queryWrapper2 = new QueryWrapper();
        queryWrapper2.eq("solutionname", oldName);
        queryWrapper2.eq("account", solution.getAccount());
        Solution oldSolution = solutionMapper.selectOne(queryWrapper2);
        oldSolution.setSolutionname(solution.getSolutionname());

        int result = solutionMapper.updateById(oldSolution);
        if(result > 0){
            QueryWrapper queryWrapper3 = new QueryWrapper();
            queryWrapper3.eq("solution", oldName);
            queryWrapper3.eq("account", solution.getAccount());

            List<Task> list = taskMapper.selectList(queryWrapper3);
            for (Task task : list) {
                task.setSolution(solution.getSolutionname());
                taskMapper.updateById(task);
            }
        }
        return result;
    }

    /**
     * 删除方案
     * @param solution
     * @return
     */
    @Override
    @Transactional
    public int deleteSolution(Solution solution) {
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("solutionname", solution.getSolutionname());
        queryWrapper.eq("account", solution.getAccount());
        Solution s = solutionMapper.selectOne(queryWrapper);
        if(s.getIsaction() == 1){
            //被删除的是当前执行的方案
            return -1;
        }


        int result = solutionMapper.delete(queryWrapper);

        if(result > 0){
            QueryWrapper queryWrapper2 = new QueryWrapper();
            queryWrapper2.eq("solution", solution.getSolutionname());
            queryWrapper2.eq("account", solution.getAccount());

            taskMapper.delete(queryWrapper2);
        }

        return result;
    }

    /**
     * 根据分控账号删除方案
     * @param account
     * @return
     */
    @Transactional
    @Override
    public int deleteByAccount(String account) {
        Map<String, Object> map = new HashMap<>();
        map.put("account", account);
        return solutionMapper.deleteByMap(map);
    }

    /**
     * 执行方案
     * @param solution
     * @return
     */
    @Override
    @Transactional
    public int execSolution(Solution solution) {
        //原来执行的方案设置为非执行
        QueryWrapper queryWrapper1 = new QueryWrapper();
        queryWrapper1.eq("isaction", 1);
        queryWrapper1.eq("account", solution.getAccount());
        Solution s1 = solutionMapper.selectOne(queryWrapper1);
        s1.setIsaction(0);
        solutionMapper.updateById(s1);


        //修改原来的方案
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("solutionname", solution.getSolutionname());
        queryWrapper.eq("account", solution.getAccount());
        Solution s = solutionMapper.selectOne(queryWrapper);
        s.setIsaction(1);

        return solutionMapper.updateById(s);
    }
}
